package cn.lhf.cd.controller;

import cn.lhf.cd.annotation.FXController;
import cn.lhf.cd.service.UserService;
import cn.lhf.cd.utils.StringUtils;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.math.BigDecimal;
import java.net.URL;
import java.util.ResourceBundle;

@FXController(path = "/ui/withdrawal_other.fxml")
public class WithdrawalOtherController extends ChildrenController {
    public TextField moneyField;
    public Label tipLabel;

    @Override
    public String getTitle() {
        return "现金提取窗口";
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        moneyField.textProperty().addListener((observable, oldValue, newValue) -> {
            if (!newValue.matches("\\d*")) {
                moneyField.setText(newValue.replaceAll("[^\\d]", ""));
            }
        });
    }

    @Override
    public void onBtnLeft4() {
        String str = moneyField.getText();
        if (StringUtils.isEmpty(str)) {
            sendTip(tipLabel, "请输入金额！");
            return;
        }
        if (!str.endsWith("00")) {
            sendTip(tipLabel, "请输入100的整数倍！");
            return;
        }
        BigDecimal money = new BigDecimal(str);
        try {
            UserService.withdraw(getMainController().getSelfId(), money);
            getMainController().setFrameView(SuccessController.class, "现金提取成功，%d秒后将返回主界面");
        } catch (Exception e) {
            sendTip(tipLabel, e.getMessage());
        }
    }

    @Override
    public void onBtnRight4() {
        getMainController().goHome();
    }

}
